<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\business\EhrHelper;

/* @var $this yii\web\View */
/* @var $searchModel common\models\qywx\QywxUserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Qywx Users');
$this->params['breadcrumbs'][] = $this->title;
?>
<style type="text/css">
    .search-options {
        cursor: pointer;
    }
    .pagination {
        margin: 0px;
    }

</style>

<div class="qywx-user-index">
    <?php \yii\widgets\Pjax::begin(['id' => 'qywx_users']) ?>
    <section class="panel panel-default section-search">
        <header class="panel-heading search-options"><?= Yii::t('app', 'Search Conditions')?>
            <i class="fa fa-arrow-circle-down text-danger"></i>
        </header>
        <div class="panel-body search-options-content">
            <?= $this->render('_search', ['model' => $searchModel]); ?>
        </div>
    </section>
    <div class="box box-default">
        <div class="box-header with-border">
            <h3 class="box-title"><?= $this->title?></h3>
            <div class="box-tools pull-right">
                <?= Html::button(Yii::t('app', 'Delete'), [
                    'class' => 'btn btn-danger btn-sm',
                    'onclick' => 'on_delete_btn_clicked()'
                ]) ?>
                <?= Html::a(Yii::t('app', 'Create'), ['create'], ['class' => 'btn btn-primary btn-sm']) ?>
            </div>
        </div>
        <div class="box-body">
            <div class="table-responsive">
                <?php
                try {
                    $gv = GridView::widget([
                        'id' => 'qywxUserGrid',
                        'tableOptions' => ['class' => 'table table-condensed table-bordered table-striped table-hover'],
                        'pager'        => [
                            //            'class'          => GoPager::className(),
                            'nextPageLabel'  => '下一页',
                            'prevPageLabel'  => '上一页',
                            'firstPageLabel' => '首页',
                            'lastPageLabel'  => '末页',
                            'options'        => [
                                'class' => 'pagination pagination-sm m-t-none m-b-none',
                            ],
                        ],

                        'dataProvider' => $dataProvider,
                        'filterModel' => $searchModel,
                        'columns' => [
                            [
                                'class' => 'yii\grid\SerialColumn',
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 25px;'],
                            ],
                            [
                                'class' => 'yii\grid\CheckboxColumn',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 30px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 30px;'],
                                'name' => 'userid',
                            ],
                            [
                                'attribute' => 'id',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => '头像',
                                'format' => [
                                    'image',
                                    [
                                        'width'=>'35',
                                        'height'=>'35'
                                    ]
                                ],
                                'value' => function ($model) {
                                    return $model->avatar;
                                },
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'attribute' => 'userid',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'attribute' => 'name',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Employee Number'),
                                'attribute' => 'number',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Mobile'),
                                'attribute' => 'mobile',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'attribute' => 'department',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'attribute' => 'department_name',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 180px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 180px;'],
                            ],
                            [
                                'attribute' => 'position',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'attribute' => 'gender',
                                'value' => function ($model, $key, $index, $column) {
                                    return EhrHelper::getGenderByIntValue($model['gender']);
                                },
                                'filter' => EhrHelper::getGender_map(),
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Email'),
                                'attribute' => 'email',
                                'format' => 'email',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'attribute' => 'is_leader',
                                'value' => function ($model, $key, $index, $column) {
                                    return EhrHelper::getQywxUserIsLeaderByIntValue($model->is_leader);
                                },
                                'filter' => EhrHelper::getQywxUserIsLeader_map(),
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            // 'telephone',
                            [
                                'attribute' => 'english_name',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'attribute' => 'order',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            // 'extattr',
                            // 'status',
                            // 'memo',
                            // 'created_by',
                            // 'created_at',
                            // 'updated_by',
                            // 'updated_at',
                            [
                                'attribute' => 'updated_at',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],

                            ['class' => 'yii\grid\ActionColumn'],
                        ],
                    ]);
                } catch (Exception $e) {
                }
                ?>
                <?= $gv ?>
            </div>
        </div>
        <div class="box-footer with-border">
        </div>
    </div>
    <?php \yii\widgets\Pjax::end() ?>
</div>
<script>
    <?php $this->beginBlock('js_running')?>
    $(function () {
        $('.search-options').click(function () {
            if ($('.search-options-content').is(':visible')) {
                $(this).find('i').addClass('fa-arrow-circle-up').removeClass('fa-arrow-circle-down');
                $('.search-options-content').slideUp();
            } else {
                $(this).find('i').addClass('fa-arrow-circle-down').removeClass('fa-arrow-circle-up');
                $('.search-options-content').slideDown();
            }
        });

        $('.reset').bind('click', function () {
            $('#qywxUserGrid').find(':text').each(function () {
                $(this).val('');
            });
            $('#qywxUserGrid').find('select').each(function () {
                $(this).val('');
                $(this).change();
            });
            return true;
        });
    });

    function on_delete_btn_clicked() {
        $keys = $('#qywxUserGrid').yiiGridView('getSelectedRows');
        if ($keys.length === 0){
            WarningDlg.alert('您没有选择任何项噢！');
            return;
        }

//        $arr4delte = [];
//        $keys.forEach(function(value, index,arr){
//            $rowItem = arr[index];
//            $arr4delte.push($rowItem['qywx_user_userid']);
//        });
//        $arr4deleteStr = $arr4delte.join(',');
//        alert($arr4deleteStr);

//        $arr = JSON.stringify($arr4delte);

        $postData = 'ids4Delete=' + $keys.join();
//        alert($postData);

        krajeeDialog.confirm("您确定要删除这些项吗?", function (result) {
            if (result) {
                $.ajax({
                    type: 'post',
                    url: 'batch-delete',
                    data: $postData,
                    success: function(data){
                        var result = JSON.parse(data);
                        if (result.code === 200){
                            SuccessDlg.alert(result.message);
                            $.pjax.reload({container:"#qywx_users"});
                        }
                        else {
                            ErrorDlg.alert(result.message);
                        }
                    },
                });
            }
        });
    }
    <?php $this->endBlock()?>
</script>
<?php
$this->registerJs($this->blocks['js_running'], \yii\web\View::POS_END)
?>
